package org.bee.controller;

import org.bee.ResultTool;
import org.bee.enums.ResultCode;
import org.bee.exception.ExceptionUtils;
import org.bee.model.Element;
import org.bee.model.IdList;
import org.bee.model.result.Result;
import org.bee.secuirty.annotation.ApiAuth;
import org.bee.service.ElementService;
import org.bee.service.RoleElementService;
import org.bee.transaction.TransUtils;
import org.bee.transaction.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色元素权限管理
 */
@RestController
@RequestMapping("/roleElement")
public class RoleElementController {
    private static final Logger logger = LoggerFactory.getLogger(RoleElementController.class);

    private final ElementService elementService;

    private final RoleElementService roleElementService;

    public RoleElementController(ElementService elementService,
                                 RoleElementService roleElementService) {
        this.elementService = elementService;
        this.roleElementService = roleElementService;
    }

    /**
     * 获取角色菜单权限
     * @return 角色菜单权限
     */
    @GetMapping("getRoleElement")
    @ApiAuth(value = "roleElement.getRoleElement",description = "角色菜单权限")
    public Result getRoleElement(String roleId){
        Result result = ResultTool.success();
        //查角色拥有的元素权限信息
        List<Element> elementByRoleId = elementService.getElementByRoleId(roleId);
        List<String> elementList = new ArrayList<>();
        for (Element element : elementByRoleId) {
            //根节点(元素的id 不为parentId)
//            List<Element> list = elementByRoleId.stream().filter(elementin -> element.getElementId()
//                    .equals(elementin.getParentId())).toList();
//            if(list.isEmpty()){
//
//            }
            elementList.add(element.getElementId());
        }
        result.setData(elementList);
        return result;
    }

    /**
     * 保存角色权限
     * @param idList 角色id和元素id集合
     * @return 保存结果
     */
    @PostMapping("saveRoleElements")
    @ApiAuth(value = "roleElement.saveRoleElements",description = "保存角色权限")
    public Result saveRoleElements(@RequestBody IdList<String> idList) {
        Result result = ResultTool.success();
        if(idList == null || idList.getId() == null || idList.getList() == null){
            result = ResultTool.fail(ResultCode.PARAM_NOT_COMPLETE);
            return result;
        }
        Transaction trans = null;
        try {
            trans= TransUtils.getTrans();
            trans.begin();
            roleElementService.deleteByRoleId(idList.getId());
            List<String> list = idList.getList();
            for (String elementId : list) {
                roleElementService.saveRoleElement(idList.getId(),elementId);
            }
            trans.commit();
        } catch (Exception e) {
            TransUtils.rollback(trans);
            result = ResultTool.fail();
            logger.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            return result;
        }
        return result;
    }
}
